//
//  TTZhuanJianRightView.m
//  Estate
//
//  Created by houzhen on 2018/9/6.
//  Copyright © 2018年 ttsx. All rights reserved.
//

#import "TTZhuanJianRightView.h"
#import "TTZhuanJianRightCell.h"
#import "SXheadView.h"

@implementation TTZhuanJianRightView
-(NSMutableArray *)seleceArr{
    if (!_seleceArr) {
        _seleceArr = [NSMutableArray array];
    }
    return _seleceArr;
}
-(NSMutableArray *)seleceStrArr{
    if (!_seleceStrArr) {
        _seleceStrArr = [NSMutableArray array];
    }
    return _seleceStrArr;
}

-(NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray  = [NSMutableArray array];
    }
    return  _btnArray;
}
- (instancetype)initWithFrame:(CGRect)frame dataArr:(NSMutableArray *)dataArr{
    if(self=[super initWithFrame:frame]){
        self.dataArr = dataArr;
        self.btnArray = [NSMutableArray arrayWithArray:dataArr];
        UIButton *coverBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        coverBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [coverBtn addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:coverBtn];

        [self addSubview:self.collectionView];
        [self addSubview:self.cancelBtn];
        [self addSubview:self.sureBtn];
    }
    return self;
}
-(void)coverBtnClick{
    [self clickCan];
}
- (UIButton *)cancelBtn{
    if(_cancelBtn==nil){
        _cancelBtn=[[UIButton alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*0.15, DEF_SCREEN_HEIGHT-50, DEF_SCREEN_WIDTH*0.85/2-1, 50)];
        _cancelBtn.backgroundColor=[TTTheme defaultManger].selectColor;
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    }
    return _cancelBtn;
}

- (UIButton *)sureBtn{
    if(_sureBtn==nil){
        _sureBtn=[[UIButton alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*0.15 + DEF_SCREEN_WIDTH*0.85/2, DEF_SCREEN_HEIGHT-50,DEF_SCREEN_WIDTH*0.85/2, 50)];
        _sureBtn.backgroundColor=[TTTheme defaultManger].selectColor;
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    
    return _sureBtn;
}
- (UICollectionView *)collectionView{
    if(_collectionView==0){
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
        //组头高
        flowLayout.headerReferenceSize = CGSizeMake(0, 40);
        
        //设置行间距-最小行间距
        flowLayout.minimumLineSpacing=5;
        //设置每单元格左右间距
        flowLayout.minimumInteritemSpacing=5;
        
        //滑动的方向
        flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
        //设置view的周边间距
        flowLayout.sectionInset=UIEdgeInsetsMake(5, 5, 5, 5);
        //设置单元格的大小
        flowLayout.itemSize=CGSizeMake((DEF_WIDTH(self)-25)/4, 30);
        //设置滚动方向
        flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
        //设置fram
        self.collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*0.15, 0, DEF_WIDTH(self) *0.85, DEF_HEIGHT(self)) collectionViewLayout:flowLayout];
        //设置collection
        self.collectionView.showsVerticalScrollIndicator=NO;
        self.collectionView.backgroundColor=[UIColor whiteColor];
        
        
        self.collectionView.delegate=self;
        self.collectionView.dataSource=self;
        [self.collectionView registerClass:[TTZhuanJianRightCell class] forCellWithReuseIdentifier:@"Cell"];
        //注册头部
        [self.collectionView registerClass:[SXheadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"];
        //添加事件
        [self.cancelBtn addTarget:self action:@selector(clickCan) forControlEvents:UIControlEventTouchUpInside];
        [self.sureBtn addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectionView;
}
#pragma mark-UICollectionDataSoure
//返回组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//返回每组个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //项目人员数
    return self.btnArray.count;
    
}

//返回内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TTZhuanJianRightCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *userDic=[self.btnArray objectAtIndex:indexPath.row];
    [cell.btn setTitle:userDic[@"username"] forState:UIControlStateNormal];
  
    //点击事件
    WEAKSELF
    cell.clickBtnBlock=^(NSString *title,BOOL b){
        //判断选项数组中是否存在就移除掉
        if ([weakSelf.seleceStrArr containsObject:title]) {
            [weakSelf.seleceStrArr removeObject:title];
            [weakSelf.seleceArr removeObject:userDic];
        }else {//添加选项
            [weakSelf.seleceStrArr addObject:title];
            [weakSelf.seleceArr addObject:userDic];
        }
    };
    return cell;
}
//返回头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){
        SXheadView *header = (SXheadView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView" forIndexPath:indexPath];
        header.titleLab.text=@"人员选择";
        reusableview = header;
    }
    return reusableview;
}

#pragma mark-methold

- (void)clickCan{
    
    [UIView animateWithDuration:0.4 animations:^{
        self.frame =CGRectMake(DEF_SCREEN_WIDTH, 0,  DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    }];
}

- (void)clickSure{
    NSLog(@"确定选择%@",self.seleceArr);
    NSLog(@"确定选择%@",self.seleceStrArr);

    if(self.clickOkBtnBlock){
        self.clickOkBtnBlock(self.seleceArr);
    }
    [UIView animateWithDuration:0.4 animations:^{
        self.frame =CGRectMake(DEF_SCREEN_WIDTH, 0,  DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    }];
}

@end
